/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlleKunderGUI.java
 *
 * Created on Dec 5, 2011, 10:37:35 AM
 */
package GUILayer;

import ControlLayer.CtrCustomer;
import ModelLayer.Customer;
import java.util.ArrayList;

/**
 *
 * @author R3l4X
 */
public class AlleKunderGUI extends javax.swing.JFrame {

    /** Creates new form AlleKunderGUI */
    public AlleKunderGUI() {
        initComponents();
        setVisible(true);
        updateList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Alle Kunder");

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Kasper                     Kaspersvej 1, 9000 aalborg, 123455678", "Jimmy                      Jimmysvej 2, 9000 aalborg, 123455678", "Johannen                Johannesvej 3, 9000 aalborg, 123455678", "Christian                  Christiansvej 4, 9000 aalborg, 123455678", "Leyth                       Leythsvej 5, 9000 aalborg, 123455678", "Hansen                    Hansensvej 6, 9000 aalborg, 123455678", "Jens                        Jensenvej 7, 9000 aalborg, 123455678", "Henrik                      Henriksvej 8, 9000 aalborg, 123455678", "Lars                         Larsvej 9, 9000 aalborg, 123455678" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jButton1.setText("Luk");

        jButton2.setText("Print");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addGap(248, 248, 248))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AlleKunderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AlleKunderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AlleKunderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AlleKunderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AlleKunderGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void updateList()
    {
        CtrCustomer ctrCust = new CtrCustomer();
        ArrayList<Customer> customer = ctrCust.GetAllCustomers();
        buildList(customer);
    }
    
    private void buildList(ArrayList<Customer> customers)
    {
        String [] customer = new String [customers.size()];
        int index = 0;
        for(Customer cObj : customers)
        {
            String id = String.valueOf(cObj.getId());
            String Fname = String.valueOf(cObj.getFname());
            String lName = String.valueOf(cObj.getLname());
            String address = String.valueOf(cObj.getAddress());
            String phone = String.valueOf(cObj.getPhoneNo());
            String email    = String.valueOf(cObj.getEmail());
            if(Fname.length()>20)   
            {
                Fname = Fname.substring(0,19);
            }
            if(lName.length()>20)
            {
                lName = lName.substring(0,19);
            }
            if(email.length()>20)
            {
                email = email.substring(0,16);
            }
            customer[index] = ( id + "           " + Fname + " " + lName + "      " + address + "       " + phone + "       " + email);
            index++;
        } 
        jList1.setListData(customer);
    }
}
